package com.slz3.business.module.configManagement.conSiteCity.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conSiteCity.domain.ConSiteCity;
import com.slz3.business.module.configManagement.conSiteCity.domain.ConSiteCityFactory;
import com.slz3.business.module.configManagement.conSiteCity.steward.ConSiteCitySpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConSiteCityServiceImpl implements ConSiteCityService {

    @Override
    public ConSiteCity save(ConSiteCity conSiteCity) {
        return Repositorys.conSiteCity.save(ConSiteCityFactory.initBaseFields(conSiteCity));
    }

    @Override
    public Page<ConSiteCity> page(MultiValueMap<String, String> params) {
        Page<ConSiteCity> conSiteCityPage = Repositorys.conSiteCity.findAll(
                ConSiteCitySpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conSiteCityPage;
    }

    @Override
    public ConSiteCity modify(ConSiteCity conSiteCityNew) {
        return Repositorys.conSiteCity.saveAndFlush(ConSiteCityFactory.initBaseFields(conSiteCityNew));
    }

    @Override
    public Optional<ConSiteCity> single(UUID uuid) {
        return Repositorys.conSiteCity.findById(uuid);
    }

    @Override
    public List<ConSiteCity> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conSiteCity.findAllById(uuids);
    }

    @Override
    public List<ConSiteCity> delete(List<ConSiteCity> conSiteCityList) {
        conSiteCityList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConSiteCityFactory.initBaseFields(item);
        });
        return Services.conSiteCityTransactional.batchModify(conSiteCityList);
    }

}
